'use strict';
const _fileUtils = require('../utils/fileUtils');
const _ = require('lodash');
module.exports = class {
    constructor(app) {
        this.app = app;
        this.logger = app.logger;
        this.config = app.config;
    }

    async insert(model, data) {
        const path = this.config.dbPath + "/" + model;

        let arr = _fileUtils.readFile2json(path);
        if (arr) {
            arr = [];
        }
        arr.push(data);
        return _fileUtils.writeFile(path, arr);
    }

    async insertMany(model, array) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        arr.push(...array);
        return _fileUtils.writeFile(path, arr);
    }

// const result = await ctx.model.Setting.findOneAndUpdate({ tenantCode }, body, { upsert: true });
    async findOne(model, query) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (isFind) {
                return item;
            }
        });
    }

    async find(model, query, sort) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        const list = [];
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (isFind) {
                list.push(item);
            }
        });
        return list;
    }

    async updateOne(model, query, data) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (isFind) {
                _.map(data, (value, key) => {
                    item[key] = value;
                });
            }
        });
        return _fileUtils.writeFile(path, arr);
    }

    async updateMany(model, query, data) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (isFind) {
                _.map(data, (value, key) => {
                    item[key] = value;
                });
            }
        });
        return _fileUtils.writeFile(path, arr);
    }

    async deleteOne(model, query) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        const list = [];
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (!isFind) {
                list.push(item);
            }
        });
        return _fileUtils.writeFile(path, list);
    }


    async deleteMany(model, query) {
        const path = this.config.dbPath + "/" + model;
        const arr = _fileUtils.readFile2json(path);
        const list = [];
        _.map(arr, item => {
            let isFind = true;
            _.map(query, (value, key) => {
                if (item[key] !== value) {
                    isFind = false;
                }
            });
            if (!isFind) {
                list.push(item);
            }
        });
        return _fileUtils.writeFile(path, list);
    }
};
